home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1450.dms / var1450.adf / TrackdiskDevice / Example1.c < prev    next >
C/C++ Source or Header  |  1992-04-27  |  5KB  |  178 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Devices                 Amiga C Club       */
  7. /* Chapter: Trackdisk Device            Tulevagen 22       */
  8. /* File:    Example1.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-27                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20.  
  21.  
  22. /* This program will use the Trackdisk Device to turn on */
  23. /* and off the internal diskdrive's motor.               */
  24.  
  25.  
  26.  
  27. #include <exec/types.h>        /* STRPTR         */
  28. #include <exec/ports.h>        /* struct Message */
  29. #include <exec/nodes.h>        /* NT_MESSAGE     */
  30. #include <devices/trackdisk.h> /* Trackdisk Device */
  31.  
  32.  
  33.  
  34. /* Diskdrive: */
  35. #define DF0 0
  36. #define DF1 1
  37. #define DF2 2
  38. #define DF3 3
  39.  
  40. /* Motor status: */
  41. #define OFF  0
  42. #define ON   1
  43.  
  44.  
  45.  
  46. /* Declare a pointer to our reply port: */
  47. struct MsgPort *replymp;
  48.  
  49. /* Declare a pointer to a IOStdReq structure:      */
  50. /* (We do not use the extended IOExtTD structure.) */
  51. struct IOStdReq *req;
  52.  
  53. /* Have we opened the Trackdisk Device? */
  54. BOOL error = TRUE;
  55.  
  56.  
  57.  
  58. /* Declare our functions: */
  59. void main();
  60. void clean_up( STRPTR text );
  61. BYTE DiskMotor( BYTE status );
  62.  
  63.  
  64.  
  65. void main()
  66. {
  67.   /* Was the motor already on or not? */
  68.   BYTE motor_was_on;
  69.  
  70.  
  71.  
  72.   /* Get a reply port: */
  73.   replymp = (struct MsgPort *)
  74.     CreatePort( NULL, 0 );
  75.   if( !replymp )
  76.     clean_up( "Could not create the reply port!" );
  77.  
  78.   /* Create an IOStdReq structure: */
  79.   req = (struct IOStdReq *)
  80.     CreateStdIO( replymp );
  81.   if( !req )
  82.     clean_up( "Could not create the IO!" );
  83.  
  84.   /* Open the Trackdisk Device: */
  85.   error = OpenDevice( TD_NAME, DF0, req, 0 );
  86.   if( error )
  87.     clean_up( "Could not open the Trackdisk Device!" );
  88.  
  89.  
  90.  
  91.   /* Turn the motor on: */
  92.   motor_was_on = DiskMotor( ON );
  93.  
  94.  
  95.   /* Only if the motor was off before we turned it on should */
  96.   /* we turn it off again. If the motor was already on we    */
  97.   /* should not turn it off since that means that some other */
  98.   /* task wants it to be on.                                 */
  99.   if( !motor_was_on )
  100.     DiskMotor( OFF );
  101.  
  102.  
  103.  
  104.   /* Clean up and quit: */
  105.   clean_up( "The End!" );
  106. }
  107.  
  108.  
  109.  
  110. /* Close and return everything that has been */
  111. /* opened and  allocated before we quit:     */
  112.  
  113. void clean_up( STRPTR text )
  114. {
  115.   /* Close the Trackdisk Device: */ 
  116.   if( !error )
  117.     CloseDevice( req );
  118.  
  119.   /* Delete the IOStdReq structure: */
  120.   if( req )
  121.     DeleteStdIO( req, sizeof( struct IOStdReq ) );
  122.  
  123.   /* Remove the replyport: */
  124.   if( replymp )
  125.     DeletePort( replymp);
  126.  
  127.   /* Print the message: */
  128.   printf( "%s\n", text );
  129.  
  130.   /* Quit: */
  131.   exit( 0 );
  132. }
  133.  
  134.  
  135.  
  136. /* DiskMotor() will turn on/off the diskdrive's motor.  */
  137. /*                                                      */
  138. /* Synopsis:  oldstatus = DiskMotor( new_status );      */
  139. /*                                                      */
  140. /* oldstatus: (BYTE) DiskMotor() returns 1 if the motor */
  141. /*            was already on, else 0 (motor was off).   */
  142. /*                                                      */
  143. /* newstatus: (BYTE) Set this field to 1 (ON) if you    */
  144. /*            want to turn the motor on, else set it to */
  145. /*            0 (OFF) and the motor will be turned off. */
  146. /*            Note! If you turned the motor on when the */
  147. /*            motor was off you must turn it off before */
  148. /*            your program terminates. However, if it   */
  149. /*            was already on you should not turn it     */
  150. /*            off.                                      */
  151.  
  152. BYTE DiskMotor( BYTE status )
  153. {
  154.   /* Will the user what we will do: */
  155.   printf( "Motor will be turned: %s\n", status ? "On" : "Off" );
  156.  
  157.   /* Set our request: */
  158.   req->io_Command = TD_MOTOR; /* Turn motor on/off. */ 
  159.   req->io_Length = status;    /* 0 = off, 1 = on.   */
  160.  
  161.   /* Do our request, and return when done: */
  162.   DoIO( req );
  163.  
  164.   /* DoIO() will return when it has done our request, */
  165.   /* so it will therefore not send us any message to  */
  166.   /* our reply port. If we had used SendIO() instead  */
  167.   /* (returns immediately) we would had to wait for a */
  168.   /* message to arrive at our reply port.             */
  169.  
  170.   /* Check if the motor was on or not before we changed it: */
  171.   printf( "Motor was: %s\n", req->io_Actual ? "On" : "Off" );
  172.  
  173.   /* Return 1 if the motor was already on, 0 if not: */
  174.   return( (BYTE) req->io_Actual );
  175. }
  176.  
  177.  
  178.